From 8233de7c5355f502eb335d00682c42e2f8dde456 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Wed, 2 Feb 2022 12:31:45 +0100
Subject: fix: handle getStaticPaths fallback

I had errors with next build because of fallback. I need to return
early if the path does not exists, if not Next complains about
undefined variables.
I don't think it was related but I also fix the paths format in
getStaticPaths, I forgot the params object in some dynamic routes.
---
 src/pages/thematique/[slug].tsx | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

(limited to 'src/pages/thematique/[slug].tsx')

diff --git a/src/pages/thematique/[slug].tsx b/src/pages/thematique/[slug].tsx
index dbef25b..df7ff1a 100644
--- a/src/pages/thematique/[slug].tsx
+++ b/src/pages/thematique/[slug].tsx
@@ -2,6 +2,7 @@ import { getLayout } from '@components/Layouts/Layout';
 import PostHeader from '@components/PostHeader/PostHeader';
 import PostPreview from '@components/PostPreview/PostPreview';
 import Sidebar from '@components/Sidebar/Sidebar';
+import Spinner from '@components/Spinner/Spinner';
 import { RelatedTopics, ThematicsList, ToC } from '@components/Widgets';
 import {
   getAllThematicsSlug,
@@ -12,6 +13,7 @@ import { NextPageWithLayout } from '@ts/types/app';
 import { ArticleMeta } from '@ts/types/articles';
 import { TopicPreview, ThematicProps } from '@ts/types/taxonomies';
 import { settings } from '@utils/config';
+import { getFormattedPaths } from '@utils/helpers/format';
 import { loadTranslation } from '@utils/helpers/i18n';
 import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
 import Head from 'next/head';
@@ -26,6 +28,8 @@ const Thematic: NextPageWithLayout<ThematicProps> = ({ thematic }) => {
   const relatedTopics = useRef<TopicPreview[]>([]);
   const router = useRouter();
 
+  if (router.isFallback) return <Spinner />;
+
   const updateRelatedTopics = (newTopics: TopicPreview[]) => {
     newTopics.forEach((topic) => {
       const topicIndex = relatedTopics.current.findIndex(
@@ -173,9 +177,10 @@ export const getStaticProps: GetStaticProps = async (
 
 export const getStaticPaths: GetStaticPaths = async () => {
   const allSlugs = await getAllThematicsSlug();
+  const paths = getFormattedPaths(allSlugs);
 
   return {
-    paths: allSlugs.map((post) => `/thematique/${post.slug}`),
+    paths,
     fallback: true,
   };
 };
-- 
cgit v1.2.3

